package org.geektimes.rest.demo;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.geektimes.rest.model.CommonRequest;
import org.geektimes.rest.model.JsonRootBean;
import org.geektimes.rest.model.PayRefundRequest;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

public class RestClientDemo {

    public static void main(String[] args) throws Exception{
//        testGet();
        testPostJson();

    }

    private static void testGet() {
        Client client = ClientBuilder.newClient();
        Response response = client
                .target("http://localhost:6004/user_web_war/hello/world")      // WebTarget
                .request() // Invocation.Builder
                .get();                                     //  Response

        String content = response.readEntity(String.class);

        System.out.println(content);
    }


    private static void testPostJson() throws JsonProcessingException {
        String url = "your url";

        String param = "{\n" +
                "\t\"userId\": \"\",\n" +
                "\t\"machineId\": \"\",\n" +
                "\t\"version\": \"\",\n" +
                "\t\"sourceType\": \"\",\n" +
                "\t\"traceId\": \"\",\n" +
                "\t\"sign\": \"44d0a1180b53fcad523ef7f49a999950\"}";

        CommonRequest request = JSONObject.parseObject(param, CommonRequest.class);
//        ObjectMapper objectMapper = new ObjectMapper();
//        String body = objectMapper.writeValueAsString(request);

        Client client = ClientBuilder.newClient();
        Response response = client
                .target(url)
                .request() // Invocation.Builder
                .header("Content-type", MediaType.APPLICATION_JSON)
                .post(Entity.json(request));                                     //  Response

        String content = response.readEntity(String.class);

        System.out.println(content);

    }

}
